package org.flexthinker.binding.validation.support
{
    import org.flexthinker.binding.validation.ValidationResults;

    import mx.events.ValidationResultEvent;

    /**
     *
     */
    public class DefaultValidationResults implements ValidationResults
    {
        private var results:Vector.<ValidationResultEvent> = new Vector.<ValidationResultEvent>;

        public function DefaultValidationResults( results:Vector.<ValidationResultEvent> = null )
        {
            if( results != null )
            {
                this.results = results;
            }
        }

        public function addAllMessages( vResults:Vector.<ValidationResultEvent> ):void
        {
            results = results.concat( vResults );
        }

        public function addMessage( event:ValidationResultEvent ):void
        {
            if( event != null )
            {
                results.push( event );
            }
        }


        public function getHasWarnings():Boolean
        {
            return getMessagesForSeverity( ValidationResultSeverity.WARNING ).length > 0;
        }


        public function getHasInfo():Boolean
        {
            return getMessagesForSeverity( ValidationResultSeverity.INFO ).length > 0;
        }


        public function getHasErrors():Boolean
        {
            return getMessagesForSeverity( ValidationResultSeverity.ERROR ).length > 0;
        }


        public function getMessagesForSeverity( severity:String ):Vector.<ValidationResultEvent>
        {
            var list:Vector.<ValidationResultEvent> = new Vector.<ValidationResultEvent>();
            for each ( var vResult:ValidationResultEvent in results )
            {
                switch( severity )
                {
                    case ValidationResultSeverity.ERROR:
                        if( vResult.type == ValidationResultEvent.INVALID )
                        {
                            list.push( vResult );
                            continue;
                        }
                        break;
                    case ValidationResultSeverity.WARNING:
                    case ValidationResultSeverity.INFO:
                        if( vResult.type == ValidationResultEvent.VALID )
                        {
                            list.push( vResult );
                        }
                        break;
                }
            }
            return list;
        }


        public function getMessageCountForSeverity( severity:String ):int
        {
            return getMessagesForSeverity( severity ).length;
        }


        public function getMessageCount():int
        {
            return getMessages().length;
        }


        public function getMessages():Vector.<ValidationResultEvent>
        {
            return results;
        }


        public function getMessagesForProperty( propertyName:String ):Vector.<ValidationResultEvent>
        {
            var list:Vector.<ValidationResultEvent> = new Vector.<ValidationResultEvent>();
            for each ( var vResult:ValidationResultEvent in results )
            {
                if( vResult.field == propertyName )
                {
                    list.push( vResult );
                }
            }
            return list;
        }


        public function getMessageCountForProperty( propertyName:String ):int
        {
            return getMessagesForProperty( propertyName ).length;
        }


        public function clearAllMessages():void
        {
            results = new Vector.<ValidationResultEvent>;
        }

        public function clearMessagesForProperty( propertyName:String ):void
        {
            for each ( var vResult:ValidationResultEvent in results )
            {
                if( vResult.field == propertyName )
                {
                    results.splice( results.indexOf(vResult), 1 );
                }
            }
        }
    }
}